/////////////////////////////////////////////////////////////////////////////
// This file is part of the "Java-DAP" project, a Java implementation
// of the OPeNDAP Data Access Protocol.
//
// Copyright (c) 2007 OPeNDAP, Inc.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
/////////////////////////////////////////////////////////////////////////////
using System;
using System.IO;
using NDODS.DAP.BaseTypes;

namespace NDODS.DAP.Server
{
	
	/// <summary> Represents a clause containing a simple value. If the value is
	/// an constant value such as "2.0", the clause's isConstant() method
	/// will return true; if it is a variable of the dataset, isConstant() will
	/// return false.
	/// 
	/// </summary>
	/// <author>  Joe Wielgosz (joew@cola.iges.org)
	/// </author>
	/// <seealso cref="ClauseFactory">
	/// </seealso>
	public class ValueClause:AbstractClause, ISubClause
	{
		/// <summary> Returns the BaseType represented by this clause.</summary>
		virtual public BaseType Value
		{
			get
			{
				return value_Renamed;
			}
			
		}
		virtual public IClause Parent
		{
			get
			{
				return parent;
			}
			
			set
			{
				this.parent = value;
			}
			
		}
		
		/// <summary> Creates a new ValueClause.
		/// 
		/// </summary>
        /// <param name="value_Renamed">   The BaseType represented by this clause. This can
		/// be either a BaseType taken from the DDS of a dataset, or a BaseType
		/// object created to hold a constant value.
		/// </param>
		/// <param name="constant">Should be set to false if the value parameter is
		/// from the DDS of a dataset, and true if the value parameter is a
		/// constant value.
		/// </param>
		protected internal ValueClause(BaseType value_Renamed, bool constant)
		{
			this.value_Renamed = value_Renamed;
			this.constant = constant;
			this.defined = constant;
			this.children = new System.Collections.ArrayList();
		}
		
		/// <summary> Returns the BaseType represented by this clause. Equivalent to
		/// getValue(), except that
		/// calling this method flags this clause as "defined".
		/// 
		/// </summary>
		/// <throws>  DAP2ServerSideException Not thrown by this type of clause. </throws>
		public virtual BaseType Evaluate()
		{
			defined = true;
			return value_Renamed;
		}
		
		/// <summary> Prints the original string representation of this clause.
		/// For use in debugging.
		/// </summary>
        public override String ToString()
        {
            if (constant)
            {
                //StringWriter w = new StringWriter();
                MemoryStream w = new MemoryStream(100);
                //UPGRADE_ISSUE: Constructor 'java.io.StreamWriter.StreamWriter' was not converted. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1000_javaioStreamWriterStreamWriter_javaioWriter'"
                value_Renamed.PrintValue(new StreamWriter(w), "", false);
                StreamReader sr = new StreamReader(w);
                return sr.ReadToEnd();
            }
            else
            {
                return value_Renamed.Name;
            }
        }
		
		protected internal BaseType value_Renamed;
		protected internal IClause parent;
	}
}